#!/usr/bin/env python
#! encoding: utf-8

import re
import os
import tempfile


class Properties:

    def __init__(self, file_name):
        self.file_name = file_name
        self.properties = {}
        try:
            fopen = open(self.file_name, 'r')
            for line in fopen:
                line = line.strip()
                if line.find('=') > 0 and not line.startswith('#'):
                    strs = line.split('=')
                    self.properties[strs[0].strip()] = strs[1].strip()
        except Exception as e:
            raise e
        else:
            fopen.close()

    def has_key(self, key):
        return key in self.properties

    def get(self, key, default_value=''):
        if key in self.properties:
            return self.properties[key]
        return default_value

    def put(self, key, value):
        self.properties[key] = value
        replace_property(self.file_name, key + '=.*', key + '=' + value, True)


def parse(file_name):
    return Properties(file_name)


def replace_property(file_name, from_regex, to_str, append_on_not_exists=True):
    tmpfile = tempfile.TemporaryFile()

    if os.path.exists(file_name):
        r_open = open(file_name, 'r')
        pattern = re.compile(r'' + from_regex)
        found = None
        for line in r_open:
            if pattern.search(line) and not line.strip().startswith('#'):
                found = True
                line = re.sub(from_regex, to_str, line)
            tmpfile.write(bytes(line, encoding='utf-8'))
        if not found and append_on_not_exists:
            tmpfile.write(b'\n' + bytes(to_str,encoding='utf-8'))
        r_open.close()
        tmpfile.seek(0)

        content = tmpfile.read()

        if os.path.exists(file_name):
            os.remove(file_name)

        w_open = open(file_name, 'wb')
        w_open.write(content)
        w_open.close()

        tmpfile.close()
    else:
        print("file %s not found" % file_name)


conf = dict(
    gc_host='192.168.240.115',
    zookeeper_host='192.168.240.15',
    zookeeper_port='2182',
    jdbc_url='//192.168.240.7:1521/HWGCDB',
    ftp_HostIp='192.168.240.15',

    cas_port='8010',
    om_port='8012',
    wfm_port='8015',
    am_port='8013',

    sysadmin='sysadmin', 

    ftp_Port='22',
    ftp_userName='portal',
    ftp_password='portal@#$2018',

    jdbc_username_om='om',
    jdbc_password_om='om_2018',
)


full_conf = dict(
    casUrl='{0}:{1}'.format(conf.get('gc_host'),conf.get('cas_port')),
    omUrl='{0}:{1}'.format(conf.get('gc_host'),conf.get('om_port')),
    wfmUrl='{0}:{1}'.format(conf.get('gc_host'),conf.get('wfm_port')),
    amUrl='{0}:{1}'.format(conf.get('gc_host'),conf.get('am_port')),
    jdbc_url='jdbc:oracle:thin:@{0}'.format(conf.get('jdbc_url')),
    zookeeper='zookeeper://{0}:{1}'.format(conf.get('zookeeper_host'),conf.get('zookeeper_port')),
)

def modify_xxx_config_properties(path):
    props = parse(path) 
    props.put('casUrl', full_conf.get('casUrl'))     

if __name__ == '__main__':
    xxx_config_properties=r'GC/config.properties'
    modify_xxx_config_properties(xxx_config_properties)

